package com.chunxuan.langquan.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chunxuan.langquan.MainActivity;
import com.chunxuan.langquan.R;
import com.chunxuan.langquan.base.Base2Activity;
import com.chunxuan.langquan.base.BaseResult2;
import com.chunxuan.langquan.base.Global;
import com.chunxuan.langquan.dao.bean.CouponBean;
import com.chunxuan.langquan.dao.http.APIRetrofit;
import com.chunxuan.langquan.support.util.Logg;
import com.chunxuan.langquan.ui.activity.store.ProductCategoryActivity;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class CouponActivity extends Base2Activity {
    private ImageView ivBack;
    private TextView tvUseRightNow,tvCouponPrice,tvCouponTitle;
    private List<CouponBean> couponBeanList=new ArrayList<>();
    @Override
    protected int getLayoutRes() {
        return R.layout.activity_my_coupon;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ivBack=findViewById(R.id.iv_back);
        tvUseRightNow=findViewById(R.id.tv_use_right_now);
        tvCouponPrice=findViewById(R.id.coupon_price);
        tvCouponTitle=findViewById(R.id.tv_coupon_title);
        getCouponList();
        setOnClickListener(ivBack,tvUseRightNow);
    }
    public Disposable getCouponList() {
        return APIRetrofit.getDefault().getCouponList(Global.HEADER,"")
                .compose(RxUtil.<BaseResult2<List<CouponBean>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<CouponBean>>>() {
                    @Override
                    public void accept(BaseResult2<List<CouponBean>> baseResult2) throws Exception {
                        hideProgress();
                        if (baseResult2.isSuccess()) {
                            couponBeanList=baseResult2.getData();
                            if(null!=couponBeanList&&couponBeanList.size()>0){
                                tvCouponPrice.setText("￥"+couponBeanList.get(0).getValue());
                                tvCouponTitle.setText(couponBeanList.get(0).getTitle());
                            }
                        } else {                            ToastUtils.showShort(baseResult2.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                        ToastUtils.showShort("获取优惠券列表失败");
                    }
                });
    }
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_use_right_now:
                Intent intent = new Intent(CouponActivity.this, MainActivity.class);
                intent.putExtra("guid_id", 2);
                startActivity(intent);
                finish();
                break;
        }
    }
}
